package com.company.OfferGuide;


import com.company.struct.Node;

import java.util.HashMap;

public class 复杂链表的复制 {

    public Node copyRandomList(Node head) {
        if (head == null) return null;

        HashMap<Node,Node> hashMap = new HashMap<>();
        Node p = head;
        while (p != null) {
            hashMap.put(p,new Node(p.val));
            p = p.next;
        }

        p = head;

        while (p != null) {
            hashMap.get(p).next = hashMap.get(p.next);
            if (p.random != null) {
                hashMap.get(p).random = hashMap.get(p.random);
            }
            p = p.next;
        }

        return hashMap.get(head);
    }

}
